<?php
namespace backend\controllers;

use backend\models\ArticleSearch;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;
use backend\models\Article;
use backend\models\FilesForm;
use backend\models\Files;
use backend\models\Category;


class ArticleController extends BaseController{

	public $nodeName = '文章管理';
	public $request = '';
	public $session = '';
	public $init_upload_image = '/common/uploads/system/upload.jpg';

	public function init(){
        parent::init();
	}
	public function actionIndex(){
		$lists = Article::find()->asArray()->all();
		
		//var_dump(html_entity_decode($lists));
		/**
		 * htmlentities($value['content']);
		 *
		 */
		
		foreach ($lists as $key => $value) {
			echo htmlentities($value['content']);
		}
	}



	public function actionLists(){
	    $artM=new Article();
        $request=Yii::$app->request;
	    //获取传递过来的数据
        $where='';
        $where1='';
        if($request->isPost){
           $artM->setScenario('select');//定义场景
           $artM->load($request->post());
           if($artM->title!=''){
             $where=['like','title',$artM->title];//组装查询条件1
           }
           if($artM->status!=''){
               $where1=['status'=>$artM->status];//组装查询条件2
           }
        }
        $models=$artM->find()->where(['and',$where,$where1]);
        $page=new Pagination(['totalCount'=>$models->count(),'pageSize'=>'10']);
        $models=$models->orderBy('add_time desc')->offset($page->offset)->limit($page->limit)->asArray()->all();
        $data=[
            'model'=>$models,
            'artM'=>$artM,
            'page'=>$page,
        ];
		return $this->render('lists', ['data'=>$data]);
	}


    public function actionUpdaForm(){
        $article=new Article();
        //获取当前文章的id
        $request=Yii::$app->request;
        $id=$request->get('id');
        //获取当前id的数据
        $current_id_data=Article::findOne($id);
        if($request->isPost){
            //获取提交过来的数据


        }


        $result=[
            'article'=>$article,
            'pageTitle' => '修改文章',
            'clists' => Category::find()->all(),//分类
            'current_data'=>$current_id_data, //修改前的数据
        ];
	    return $this->render('upda-form',$result);
    }

	public function actionCreate(){

		$id = $this->request->get('id');

		if(!empty($id)){
			$article = Article::findOne($id);
			$article->setScenario('update');
		}else{
			$article = new Article;
			$article->setScenario('create');
		}

		if($this->request->isPost){
			if($article && $article->load($this->request->post())){
				$article->add_time=time();
				if($article->save()){
					$this->session->setFlash('success', '保存成功!');
					return $this->goBack();
				}else{
					$this->session->setFlash('error', '保存失败!');
				}
			}else{
				$this->session('error', '???????!!!!!');
			}

		}

		$result = [
			'article' => $article,
			'pageTitle' => '添加文章',
			'clists' => Category::find()->all(),
		];

		return $this->render('data-form', $result);
	}

	public function actionDel(){

	}

	public function actionAjaxDel(){

	}

	public function actionAjaxMoveCateogory(){
		
	}

	public function getImageUrl($param){

		$urlList = array();
		if(is_array($param)){
			$files = Files::find()->select(['path', 'name'])->where(['in', 'id', $param])->asArray()->all();

			if(!empty($files) && is_array($files)){
				foreach ($files as $value) {
					$urlList[] = '/' . trim($value['path'], '/') . '/' . $value['name'];
				}
				return $urlList;
			}
		}

		return false;
	}


	public function actionSearch(){
		Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
		$out = ['results' => ['id' => '', 'text' => '']];

		/*if (!$q) {
	        return $out;
	    }*/

	    $data = Article::find()
	                ->select('id, title AS text')
	                ->asArray()
	                ->all();
	                
	    $out['results'] = array_values($data);

	    return $out;
	}
}